package com.jxzyk.model.know;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.core.entity.I_List;
import com.core.entity.params.MapParams;
import com.core.entity.params.ReqParams;
import com.core.ui.activity.BaseActivity;
import com.jxzyk.R;
import com.jxzyk.adapter.QuestionAdapter;
import com.jxzyk.config.API;
import com.jxzyk.entity.QAList;
import com.jxzyk.entity.Question;
import com.jxzyk.http.BaseRequest.OnDialogListener;
import com.jxzyk.http.req.QARequest;
import com.jxzyk.model.EditActivity;
import com.jxzyk.model.notes.NoteDetailsActivity;
import com.jxzyk.model.notes.NotesActivity;
import com.jxzyk.view.TitleLayout;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.act_know)
public class KnowActivity extends BaseActivity {
	@ViewInject(R.id.title)
	TitleLayout title;
	@ViewInject(R.id.tv_know_qa)
	TextView tv_qa;
	@ViewInject(R.id.tv_know_time)
	TextView tv_time;
	@ViewInject(R.id.list_know)
	ListView listView;

	private QAList qaList;
	private QuestionAdapter adapter;

	@Override
	public void initData(Bundle bundle) {
		bundle = getIntent().getExtras();
		if (bundle != null) {
			qaList = (QAList) bundle.getSerializable(QAList.class.getName());
		}
	}

	private List<I_List> lists = new ArrayList<>();

	@Override
	public void initUi() {
		title.setTtile("知道");
		title.setOnLeftListener(R.drawable.ic_write, new OnClickListener() {

			@Override
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putString(EditActivity.class.getName(), API.QA.answer);
				bundle.putSerializable(qaList.getClass().getName(), qaList);
//				bundle.putString("qa_id", qaList.getQa_id());
//				bundle.putString("qa_content", qaList.getQa_content());
				// showActivity(EditActivity.class, bundle);
				Intent intent = new Intent(KnowActivity.this,
						EditActivity.class);
				intent.putExtras(bundle);
				startActivityForResult(intent, 1);

			}
		});
		if (qaList != null) {
			tv_qa.setText(qaList.getQa_content());
			tv_time.setText(qaList.getQa_createtime());
			adapter = (QuestionAdapter) new QuestionAdapter().instance(
					getBaseContext(), lists);
			listView.setAdapter(adapter);
			loadQuestion(qaList.getQa_id());
		}
	}

	private void loadQuestion(String qa_id) {
		lists.clear();
		QARequest request = new QARequest(KnowActivity.this, API.QA.getQuestion);
		ReqParams params = new MapParams();
		params.put("qa_id", qa_id);
		request.setParams(params);
		request.setOnDialogListener(new OnDialogListener() {

			@Override
			public void onSuccess(JSONObject json) {
				JSONArray array = json.getJSONArray("data");
				try {
					if (array.size() > 1) {
						for (int i = 1; i < array.size(); i++) {
							Question question = (Question) new Question()
									.instance(array.getJSONObject(i));
							lists.add(question);
						}
					}
					adapter.notifyDataSetChanged();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(String msg) {
				lists.clear();
				adapter.notifyDataSetChanged();
			}
		});
		request.show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 1:
			loadQuestion(qaList.getQa_id());
			break;

		default:
			break;
		}
	}
}
